package com.ebanca.drawme;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;

import com.ebanca.drawme.R;
 
 /**
  * Class that implements the layout container for MapView.
  * In this way it is offered a flexible centered layout 
  * for the map.
  */
public class MapParent extends LinearLayout implements
		ViewMenuDirectionNavigation {

	private Bitmap image;
	private MapView mapFrame;

	public MapParent(Context context) {
		super(context);

	}

	public MapParent(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		this.setBackgroundColor(getResources()
				.getColor(R.color.backgroundColor));
		this.setGravity(Gravity.CENTER);
		this.setPadding(0, 0, 0, 0);
	}

	/** 
	 * The scale of the MapView is computed so we can provide it's dimensions
	 * before drawing it.
	 * 
	 * The images 'defaultfloor', 'marker_street_x', 'marker_street_y' 
	 * are loaded because they are needed hen measuring the dimensions
	 * of the MapView, and are passed to MapView for preventing them 
	 * to be loaded again.
	 * 
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (image == null) {
			image = BitmapFactory.decodeResource(getResources(),
					R.drawable.gasca);
			Bitmap streetH = BitmapFactory.decodeResource(getResources(),
					R.drawable.marker_street_x);
			Bitmap streetV = BitmapFactory.decodeResource(getResources(),
					R.drawable.marker_street_y);

			double scale = (getWidth()- streetV.getWidth() * 2.0)/ (image.getWidth()+30);
			double scaleY = (getHeight()-streetH.getHeight() * 2.0)/ image.getHeight();
			if (scaleY < scale && scaleY > 1.0) {
				scale = scaleY;
			}

			mapFrame = new MapView(getContext(), scale);
			mapFrame.image = image;
			mapFrame.streetH = streetH;
			mapFrame.streetV = streetV;
			mapFrame.setLayoutParams(new LayoutParams(
					(int) ((image.getWidth()+30) * scale + streetV.getWidth() * 2.0),
					(int) (image.getHeight() * scale + streetH.getHeight() * 2.0)));
			mapFrame.init();
			this.addView(mapFrame);
		}
	}
	
	/**
	 * Implementation of the methods from ViewMenuDirectionNavigation interface. 
	 */

	@Override
	public void onDown() {
		mapFrame.onDown();

	}

	@Override
	public void onLeft() {
		mapFrame.onLeft();

	}

	@Override
	public void onRight() {
		mapFrame.onRight();

	}

	@Override
	public void onUp() {
		mapFrame.onUp();

	}

	@Override
	public void onEnter() {
		
	}
}
